package com.pengpeng.redis.service;

import com.pengpeng.redis.config.RedisProperties;
import com.pengpeng.redis.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author pengp
 * @date 2022年03月31日 16:50
 */
@Service
@Slf4j
public class MessageService {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisProperties redisProperties;

    /**
     * redis发布消息
     *
     * @param message
     */
    public void sendTopicMessage(String message) {
        redisTemplate.convertAndSend(redisProperties.getChannelName(), message);
    }

    /**
     * 处理redis消息队列
     */
    public void dealMsg() {
        String message = redisUtil.rightPop(redisProperties.getQueueName(), 1, TimeUnit.MINUTES);
        if (message != null) {
            log.info("redis--queue:" + redisProperties.getQueueName() + "  body:" + message);
        }
    }

    /**
     * @param message
     */
    public void sendQueueMessage(String message) {
        redisUtil.leftPush(redisProperties.getQueueName(), message);
    }
}

